package 综合应用.T7;

import cn.itcast.xml.XmlUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class T7 {

    public static void main(String[] args) {

//        ZhuCe();
//        Date login = Login();

//        FtPwd();
//        LookCategory();
//        LookShop();
//        getShopInfo();
        show();



    }

    // 七、
    public static void show(){
        Date date = new Date();
        Date login = Login();
        System.out.println(login.getTime());
        while (true){
            if (login.getTime()-date.getTime()==300000){
                return;
            }
        }
    }

    // 六、
    public static void getShopInfo(){
        List<Product> productXml = getProductXml();
        List<ProductDetails> pd = getPD();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入商品类别id");
        String id = sc.nextLine();
        for (Product p : productXml) {
            if (p.getCid().equals(id)){
                System.out.println(p);
            }
        }
        System.out.println("请输入商品id");
        String Cid = sc.nextLine();
        for (ProductDetails p : pd) {
            if (p.getProductId().equals(Cid)){
                System.out.println(p);
            }
        }
    }

    // 五、
    public static void LookShop(){
        List<Product> productXml = getProductXml();
        for (Product p : productXml) {
            System.out.println("("+p.getId()+")  "+p.getPname());
        }
    }

    // 四、
    public static void LookCategory(){
        List<Category> categoryXml = getCategoryXml();
        for (Category c : categoryXml) {
            System.out.println("("+c.getId()+")  "+c.getCname());
        }
    }

    // 三、
    public static void FtPwd(){
        List<User> user = getUser();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        for (User u : user) {
            if (u.getUsername().equals(username)){
                System.out.println("请输入手机号：");
                String phone = sc.nextLine();
                for (User u1 : user) {
                    if (u1.getPhone().equals(phone)){
                        System.out.println("请输入密码：");
                        String pwd = sc.nextLine();
                        System.out.println("请确认密码：");
                        String Cpwd = sc.nextLine();
                        if (pwd.equals(Cpwd)){
                            u1.setPassword(Cpwd);
                            XmlUtils.write("综合应用/T7/user.xml",user,false);
                            System.out.println("修改成功");
                            return;
                        }
                    }
                }
                System.out.println("该手机号错误...");
                return;
            }
        }
        System.out.println("没有该用户，请注册...");
    }

    // 二、
    public static Date Login(){

        List<User> user = getUser();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入手机号：");
        String phone = sc.nextLine();
        System.out.println("请输入密码：");
        String pwd = sc.nextLine();

        Date date = new Date();


        for (User u : user) {
            if ((u.getUsername().equals(username)&&u.getPassword().equals(pwd))||(u.getPhone().equals(phone)&&u.getPassword().equals(pwd))){
                System.out.println("用户名、密码正确，大数据商城欢迎您");
                return date;
            }
        }
        System.out.println("用户名、密码错误，请重新登录");
        return null;
    }

    // 一、
    public static void ZhuCe() {
        List<User> user = getUser();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入手机号：");
        String phone = sc.nextLine();

        List<User> list = new ArrayList<>();
        int count = user.size();
        count++;
        for (User u : user) {
            if (u.getUsername().equals(username)) {
                System.out.println("用户名已存在");
                return;
            }
            if (u.getPhone().equals(phone)) {
                System.out.println("该手机号已被注册");
                return;
            }
        }

        System.out.println("请输入密码：");
        String pwd = sc.nextLine();

        list.add(new User(String.valueOf(count),username,pwd,phone,"普通用户",0.0));
        XmlUtils.write("综合应用/T7/user.xml",list,true);
        System.out.println("注册成功");
    }

    // User 集合
    public static List<User> getUser() {
        return XmlUtils.readAll("综合应用/T7/user.xml", User.class);
    }

    // Product 集合
    public static List<Product> getProXml(){
        List<Product> productXml = getProductXml();
        List<Category> list = getproductList();
        for (Product p : productXml) {
            for (Category c : list) {
                p.setCategory(c);
            }
        }
        return productXml;
    }

    // productList
    public static List<Category> getproductList(){
        List<Product> productXml = getProductXml();
        List<Category> categoryXml = getCategoryXml();
        for (Category c : categoryXml) {
            ArrayList<Product>list = new ArrayList<>();
            for (Product p : productXml) {
                if (c.getId().equals(p.getCid())){
                    list.add(p);
                }
            }
            c.setProductList(list);
        }
        return categoryXml;
    }

    // ProductDetails 集合
    public static List<ProductDetails> getPD() {
        List<Product> productXml = getProductXml();
        List<ProductDetails> productDetails = XmlUtils.readAll("综合应用/T7/productDetails.xml", ProductDetails.class);
        for (Product p : productXml) {
            for (ProductDetails pp : productDetails) {
                if (p.getId().equals(pp.getProductId())){
                    pp.setProduct(p);
                }
            }
        }
        return productDetails;
    }

    // Product 集合
    public static List<Product> getProductXml() {
        List<Product> list = XmlUtils.readAll("综合应用/T7/product.xml", Product.class);
        List<Category> categoryXml = getCategoryXml();
        for (Category c : categoryXml) {
            for (Product p : list) {
                if (c.getId().equals(p.getCid())){
                    p.setCategory(c);
                }
            }
        }

        return list;
    }

    // Category 集合
    public static List<Category> getCategoryXml() {

        return XmlUtils.readAll("综合应用/T7/category.xml", Category.class);
    }
}
